package com.event.management.frontend.controller;

import com.event.management.frontend.common.Result;
import com.event.management.frontend.service.ArkAiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "AI聊天接口")
@RestController
@RequestMapping("/api/front/ai")
@Slf4j
public class AiChatController {

    @Autowired
    private ArkAiService arkAiService;

    /**
     * AI聊天API
     *
     * @param message 用户消息
     * @return AI回复
     */
    @ApiOperation(value = "AI聊天", notes = "发送消息给AI获取回复")
    @PostMapping("/chat")
    public Result<String> chat(
            @ApiParam(value = "用户消息", required = true)
            @RequestParam(required = false) String message,
            @ApiParam(value = "用户消息（JSON方式）", required = false)
            @RequestBody(required = false) ChatRequest chatRequest
    ) {
        log.info("收到AI聊天请求");
        
        // 获取消息内容（支持form表单和JSON两种方式）
        String userMessage = message;
        if (userMessage == null && chatRequest != null) {
            userMessage = chatRequest.getMessage();
        }
        
        // 参数校验
        if (userMessage == null || userMessage.trim().isEmpty()) {
            return Result.fail("消息内容不能为空");
        }
        
        log.info("用户消息: {}", userMessage);
        try {
            String response = arkAiService.processMessage(userMessage);
            return Result.ok(response);
        } catch (Exception e) {
            log.error("AI处理消息失败", e);
            return Result.fail("AI处理消息失败：" + e.getMessage());
        }
    }
}

/**
 * 聊天请求参数（用于JSON方式）
 */
class ChatRequest {
    private String message;
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
} 